(function(jQuery) {

	var funNew = null;// 调用四级地址时需要条用其他方法

	/*
	 * 四级联动插件,一个页面中只能有一组四级地址，class添加“province,city,town,street”分别添加到的四个框中
	 */
	$.AddressUtils = {

		test : function() {
			alert("aaaaaaa222333");
		},

		init : function() {
			var fourLevelAddress = new Object();
			fourLevelAddress.provinceNode = $(".province");
			fourLevelAddress.cityNode = $(".city");
			fourLevelAddress.townNode = $(".town");
			fourLevelAddress.streetNode = $(".street");

			$.AddressUtils.initProvinceNode(fourLevelAddress);
			$.AddressUtils.initCityNode(fourLevelAddress);
			$.AddressUtils.initTownNode(fourLevelAddress);
			$.AddressUtils.initStreetNode(fourLevelAddress);
		},
		renderFourLevelAddress : function(provinceNode, cityNode, townNode,
				streetNode) {
			var fourLevelAddress = new Object();
			fourLevelAddress.provinceNode = provinceNode;
			fourLevelAddress.cityNode = cityNode;
			fourLevelAddress.townNode = townNode;
			fourLevelAddress.streetNode = streetNode;

			$.AddressUtils.initProvinceNode(fourLevelAddress);
			$.AddressUtils.initCityNode(fourLevelAddress);
			$.AddressUtils.initTownNode(fourLevelAddress);
			$.AddressUtils.initStreetNode(fourLevelAddress);
		},
		// 初始化省节点
		initProvinceNode : function(fourLevelAddress) {
            if (fourLevelAddress.provinceNode == null) {
                return;
            }
			// 设置省下拉
			$.AddressUtils
					.initProvinceNode_option(fourLevelAddress.provinceNode);
			// 设置省选择事件
			$.AddressUtils.initProvinceNode_change(fourLevelAddress);

		},
		// 省选择框chang事件
		initProvinceNode_change : function(fourLevelAddress) {
			fourLevelAddress.provinceNode.combobox({
				onChange : function(n, o) {
                    if (fourLevelAddress.cityNode ==null) {
                        return;
                    }

					// 清空右边“市 ，区，街”选项
					fourLevelAddress.cityNode.combobox('clear');
					// 更新市下拉
					$.AddressUtils.updateOption(fourLevelAddress.cityNode,
							'mdDcRest/getCitys.json?Login_SessionId='
									+ sessionId + '&code=' + n);

				}
			});
		},

		clearCityAnd : function(fourLevelAddress) {
			fourLevelAddress.cityNode.combobox('clear');
			fourLevelAddress.townNode.combobox('clear');
			fourLevelAddress.streetNode.combobox('clear');
		},

		// 清空右边“区，街”选项
		clearTownAnd : function(fourLevelAddress) {
			fourLevelAddress.townNode.combobox('clear');
			fourLevelAddress.streetNode.combobox('clear');
		},
		// 清空右边“区”选项
		clearStreet : function(fourLevelAddress) {
			fourLevelAddress.streetNode.combobox('clear');
		},
		// 清空下拉框
		clearSelectNode : function(selectNode) {
			selectNode.combobox('clear');
		},
		// 设置省下拉
		initProvinceNode_option : function(provinceNode) {
			provinceNode
					.combobox({
						panelHeight : '200',
						url : 'mdDcRest/getProvinces.json?Login_SessionId='
								+ sessionId,
						valueField : 'firstCode',
						textField : 'firstArea',
					});
		},
		// 初始市选择框
		initCityNode : function(fourLevelAddress) {
            if (fourLevelAddress.cityNode == null) {
                return;
            }
			// 设置市下拉
			$.AddressUtils.initCityNode_option(fourLevelAddress.cityNode);
			// 设置市选择事件
			$.AddressUtils.initCityNode_change(fourLevelAddress);
		},
		// 市选择框chang事件
		initCityNode_change : function(fourLevelAddress) {
			fourLevelAddress.cityNode.combobox({
				onChange : function(n, o) {
                    if (fourLevelAddress.townNode ==null) {
                        return;
                    }
					fourLevelAddress.townNode.combobox('clear');
					// 更新区下拉
					$.AddressUtils.updateOption(fourLevelAddress.townNode,
							'mdDcRest/getTowns.json?Login_SessionId='
									+ sessionId + '&code=' + n);
				}
			});
		},
		// 更新市下拉
		updateOption : function(node, url) {
			node.combobox('reload', url);
		},

		// 设置市下拉
		initCityNode_option : function(cityNode) {
			cityNode.combobox({
				panelHeight : '200',
				valueField : 'secondCode',
				textField : 'secondArea',
			});
		},
		// 初始化__区县
		initTownNode : function(fourLevelAddress) {
            if (fourLevelAddress.townNode ==null) {
                return;
            }
			// 设置区县下拉
			$.AddressUtils.initTownNode_option(fourLevelAddress.townNode);
			// // 设置区县选择事件
			$.AddressUtils.initTownNode_change(fourLevelAddress);
		},

		// 设置区县选择事件
		initTownNode_change : function(fourLevelAddress) {
			fourLevelAddress.townNode.combobox({
				onChange : function(n, o) {
                    if (fourLevelAddress.streetNode == null) {
                        return;
                    }
					fourLevelAddress.streetNode.combobox('clear');
					// 更新市下拉
					$.AddressUtils.updateOption(fourLevelAddress.streetNode,
							'mdDcRest/getStreets.json?Login_SessionId='
									+ sessionId + '&code=' + n);
				}
			});
		},

		// 设置区县下拉
		initTownNode_option : function(townNode) {
			townNode.combobox({
				panelHeight : '200',
				valueField : 'thirdCode',
				textField : 'thirdArea',
			});
		},
		// 初始化街道
		initStreetNode : function(fourLevelAddress) {
            if (fourLevelAddress.streetNode == null) {
                return;
            }
			// 设置街道下拉
			$.AddressUtils.initStreetNode_option(fourLevelAddress.streetNode);
		},
		initStreetNode_option : function(streetNode) {
			streetNode.combobox({
				panelHeight : '200',
				valueField : 'fouthCode',
				textField : 'fouthArea',
			});
		},

	};

})(jQuery);

// $.AddressUtils.init();
